/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.enums.BooleanTypeEnum;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.shop.api.entity.GoodsBanner;
import com.pig4cloud.pigx.shop.service.GoodsBannerService;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图
 *
 * @author pig code generator
 * @date 2023-11-14 13:49:12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/config/banner")
@Tag(name = "轮播图管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsBannerController {

	private final GoodsBannerService goodsBannerService;

	private final GoodsMaterialService materialService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param goodsBanner 轮播图
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getGoodsBannerPage(Page page, GoodsBanner goodsBanner) {
		LambdaQueryWrapper<GoodsBanner> lqw = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(goodsBanner.getBannerName())) {
			lqw.like(GoodsBanner::getBannerName, goodsBanner.getBannerName());
		}
		if (!StringUtils.isEmpty(goodsBanner.getStatus())) {
			lqw.eq(GoodsBanner::getStatus, goodsBanner.getStatus());
		}
		lqw.orderByDesc(GoodsBanner::getCreateTime);
		IPage<GoodsBanner> items = goodsBannerService.page(page, lqw);
		items.getRecords().forEach(item -> {
			if (!StringUtils.isEmpty(item.getBannerImage())) {
				item.setBannerImage(materialService.convertImageIdToUrl(item.getBannerImage()));
			}
		});

		return R.ok(items);
	}

	/**
	 * 通过id查询轮播图
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		GoodsBanner goodsBanner = goodsBannerService.getById(id);
		if (!StringUtils.isEmpty(goodsBanner.getBannerImage())) {
			goodsBanner.setBannerImage(materialService.convertImageIdToUrl(goodsBanner.getBannerImage()));
		}
		return R.ok(goodsBanner);
	}

	/**
	 * 新增轮播图
	 * @param goodsBanner 轮播图
	 * @return R
	 */
	@Operation(summary = "新增轮播图", description = "新增轮播图")
	@SysLog("新增轮播图")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('config_banner_add')")
	public R save(@RequestBody GoodsBanner goodsBanner) {
		return R.ok(goodsBannerService.save(goodsBanner));
	}

	/**
	 * 修改轮播图
	 * @param goodsBanner 轮播图
	 * @return R
	 */
	@Operation(summary = "修改轮播图", description = "修改轮播图")
	@SysLog("修改轮播图")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('config_banner_edit')")
	public R updateById(@RequestBody GoodsBanner goodsBanner) {
		return R.ok(goodsBannerService.updateById(goodsBanner));
	}

	/**
	 * 通过id删除轮播图
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除轮播图", description = "通过id删除轮播图")
	@SysLog("通过id删除轮播图")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('config_banner_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(goodsBannerService.removeById(id));
	}

	/**
	 * 获取轮播图列表
	 * @return
	 */
	@Inner(false)
	@GetMapping("/c/bannerList")
	public R getBannerList() {
		LambdaQueryWrapper<GoodsBanner> lqw = new LambdaQueryWrapper<>();
		lqw.eq(GoodsBanner::getDelFlag, DeleteFlagEnum.VALID.getType());
		lqw.eq(GoodsBanner::getStatus, BooleanTypeEnum.TRUE.getType());
		lqw.orderBy(true, true, GoodsBanner::getSort);
		List<GoodsBanner> items = goodsBannerService.list(lqw);
		items.forEach(item -> {
			if (!StringUtils.isEmpty(item.getBannerImage())) {
				item.setBannerImage(materialService.convertImageIdToUrl(item.getBannerImage()));
			}
		});
		return R.ok(items);
	}

}
